import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import com.golden.gamedev.Game;
import com.golden.gamedev.GameLoader;

import com.golden.gamedev.object.CollisionManager;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.object.Timer;
import com.golden.gamedev.object.background.ImageBackground;
import com.golden.gamedev.util.ImageUtil;

public class BallGame extends Game {
	// creates sprites
	Sprite ballSprite;
	Sprite blockSprite;
	Sprite startSprite;
	// outcome sprites and stats
	boolean game_over;
	Sprite game_over_sprite;
	boolean start = false;
	boolean you_win = false;
	Sprite you_win_sprite;

	// sets bg bounds
	ImageBackground bg;
	BallBlockCollision collisionWBlock;
	CollisionManager collisionWWall;
	CollisionManager collisionBBall;

	// random generator
	Random r;

	// countdown that will be displayed
	int countdown;
	// level that will be displayed
	int level;
	Timer myTimer;
	GameLevels GL;

	private BufferedImage ballImages;
	SpriteGroup balls;

	public BallGame() {

		r = new Random(9999);

		// create the object
		myTimer = new Timer(100000);
		GL = new GameLevels(); // current level
		game_over = false;
	}

	public void initResources() {

		// background
		BufferedImage skyBg = getImage("Resources/skybg.gif");
		bg = new ImageBackground(skyBg, 640, 480);
		// initializes collision managers (block v. ball and ball v. wall)
		collisionWBlock = new BallBlockCollision();
		collisionWWall = new WCollisionCheck(bg);
		collisionBBall = new BallBallCollision();
		// hardware input (mouse, music, etc).
		bsInput.setMouseVisible(false);
		// Set images

		BufferedImage gameOver = getImage("Resources/game_over.jpg");
		game_over_sprite = new Sprite(gameOver, 0, 0);
		BufferedImage youWin = getImage("Resources/youwin.gif");
		you_win_sprite = new Sprite(youWin, 0, 0);

		BufferedImage ballImage = getImage("Resources/ball.png");
		ballImages = getImage("Resources/ball.png");
		BufferedImage squareImage = getImage("Resources/square.png");
		// resize images
		squareImage = ImageUtil.resize(squareImage, 20, 20);

		// sets sprite position to be the same of ball || block object
		ballSprite = new Sprite(ballImage, 0, 0);
		blockSprite = new Sprite(squareImage, 440, 320); // could of just

		// creates sprite groups
		balls = new SpriteGroup("balls");
		balls.add(ballSprite);
		SpriteGroup blocks = new SpriteGroup("blocks");
		blocks.add(blockSprite);

		// sets collision groups
		collisionWBlock.setCollisionGroup(balls, blocks);
		collisionWWall.setCollisionGroup(balls, balls);
		collisionBBall.setCollisionGroup(balls, balls);

		// sets the timer to start
		myTimer.setActive(true);
		myTimer.setCurrentTick(GL.getCurrentLevelTime());
		// initiates countdown that will be displayed
		countdown = GL.getCurrentLevelTime();
		// initiates level
		level = 1;

	}

	public void update(long elapsedTime) {
		// click to start
		if (click() && start == false) {

			ballSprite.setSpeed(0.3, 0.3);

			start = true;
		}

		if (start == true) {
			// play music
			playMusic("Resources/song.mid");
			if (myTimer.getCurrentTick() == 0 && GL.Victory()) {
				you_win = true;
			}

			// timer has to stop when sprites collide
			if (myTimer.action(1000) && myTimer.getCurrentTick() != 0) {

				countdown--; // updates countdown

				myTimer.setCurrentTick(myTimer.getCurrentTick() - 1);
			}

			if (myTimer.getCurrentTick() == 0 && !GL.Victory()) {
				GL.updateLevel();
				countdown = GL.getCurrentLevelTime(); // updates countdown
				level++; // updates level;
				myTimer.setCurrentTick(GL.getCurrentLevelTime());
				Sprite tempSprite = new Sprite(ballImages, 0, 0);

				tempSprite.setSpeed(.1 * (r.nextInt(5)), 0.1 * (r.nextInt(5)));
				balls.add(tempSprite);

			}

			// checks for collisions
			collisionBBall.checkCollision();

			collisionWBlock.checkCollision();
			if (collisionWBlock.collidedBlock) {
				game_over = true;
			}

			collisionWWall.checkCollision();

			// set block position depending on mouse

			blockSprite.setLocation(getMouseX(), getMouseY());

			// update
			blockSprite.update(elapsedTime);
			balls.update(elapsedTime);
		}
	}

	public void render(Graphics2D g) {
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, getWidth(), getHeight());

		bg.render(g);

		// render ballSprite group

		balls.render(g);

		// render blockSprite
		blockSprite.render(g);
		if (you_win) {
			// win
			you_win_sprite.render(g);
			finish();
		}
		if (game_over) {
			game_over_sprite.render(g);

			// ends game
			finish();
		}
		// change color to write timer
		g.setColor(Color.WHITE);
		// renders the countdown
		if (start == false)
			g.drawString("Click to Start", 250, 100);

		g.drawString("Time: " + Integer.toString(countdown), 320, 470);
		g.drawString("Level: " + Integer.toString(level), 320, 455);

	}

	public static void main(String[] args) {
		GameLoader game = new GameLoader();
		game.setup(new BallGame(), new Dimension(640, 480), false);
		game.start();

	}

}
